﻿var cid= null;
var commodityname = null;
var commoditypath = null;
var commodityorder = null;
var commoditystate = null;
var statebackdrop = null;
var catid = null;
var crid = $("#crid").val();
//记录页面bootstrap-table全局变量$table，方便应用
$("#rotationchartTable").bootstrapTable({
    url: reqth+"/commodity/allCommodity/"+crid,
    method: 'post',                      //请求方式（*）
    striped: true,                      //是否显示行间隔色
    toolbar: '#toolbar',                //工具按钮用哪个容器
    search: true ,                     //是否显示表格搜索
    showRefresh: true,                  //是否显示刷新按钮
    showToggle: true,                   //是否显示详细视图和列表视图的切换按钮
    pagination: true,                   //是否显示分页（*）
    sidePagination: "client",           //分页方式：client客户端分页，server服务端分页（*）
    pageList: [1,2],                    //可供选择的每页的行数（*）
    showColumns: true,                  //是否显示所有的列（选择显示的列）
    minimumCountColumns: 2,             //最少允许的列数
    columns: [{
        title: '序号',
        formatter: function one (value, row, index) {
            return index+1;
        }
    },{
        field: 'commodityname',
        title: '商品名称',
        sortable: true
    },{
        field: 'commoditypath',
        title: '图片超链接'
    },{
        field: 'commoditystate',
        title: '商品格式'
    },{
        field: 'statebackdrop',
        title: '商品格式背景'
    },{
        field: 'commodityorder',
        title: '商品状态',
        formatter : function (value, row, index) {
            if (value==0){
                return "已下架"
            }else if(value==1){
                return "已上架";
            }
        }
    },{
        field:'id',
        title: '操作',
        width: 120,
        align: 'center',
        valign: 'middle',
        formatter:function(value, row, index) {
            var id = value;
            var result = "";
            result += "<a href='javascript:;' class='btn btn-xs green' title='查看'><span class='glyphicon glyphicon-search'></span></a>";
            result += "<a href='javascript:;' class='btn btn-xs blue' title='编辑' data-toggle='modal' data-target='.bs-example-modal-lg'><span class='glyphicon glyphicon-pencil'></span></a>";
            if (row.commodityorder==0){
                result += "<a href='javascript:;' class='btn btn-xs red' title='已下架' ><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"16\" height=\"16\" fill=\"currentColor\" class=\"bi bi-toggle-off\" viewBox=\"0 0 16 16\">\n" +
                    "  <path d=\"M11 4a4 4 0 0 1 0 8H8a4.992 4.992 0 0 0 2-4 4.992 4.992 0 0 0-2-4h3zm-6 8a4 4 0 1 1 0-8 4 4 0 0 1 0 8zM0 8a5 5 0 0 0 5 5h6a5 5 0 0 0 0-10H5a5 5 0 0 0-5 5z\"/>\n" +
                    "</svg></a>"
            }else {
                result += "<a href='javascript:;' class='btn btn-xs red' title='已上架' ><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"16\" height=\"16\" fill=\"currentColor\" class=\"bi bi-toggle-on\" viewBox=\"0 0 16 16\">\n" +
                    "  <path d=\"M5 3a5 5 0 0 0 0 10h6a5 5 0 0 0 0-10H5zm6 9a4 4 0 1 1 0-8 4 4 0 0 1 0 8z\"/>\n" +
                    "</svg></a>"
            }
            return result;
        },
        events:{
            'click a[title=查看]': function (e, value, row, index) {
                layer.open({
                    type: 2,
                    title: '商品配置:'+row.commodityname,
                    shadeClose: true,
                    shade: 0.3,
                    area: ['80%', '80%'],
                    content: reqth+"commodityAllocation/goCommodityAllocation/"+row.cid
                })
                return false;
            },
            'click a[title=编辑]': function (e, value, row, index) {
                $("#myModalLabel").text("编辑");
                $("#cid").val(row.cid);
                $("#commodityname").val(row.commodityname);
                $("#commoditypath").val(row.commoditypath);
                $("#commodityorder").val(row.commodityorder);
                $("#commoditystate").val(row.commoditystate);
                $("#statebackdrop").val(row.statebackdrop);
                $("#catid").val(row.catid);
                $("#mod-but").attr("onclick","updateById()");
            },
            'click a[title=已下架]': function (e, value, row, index) {
                var loadingIndex = null;
                layer.confirm("确认上架"+row.commodityname+"吗？", {
                    icon: 3,
                    title: '提示'
                }, function(cindex) {
                    $.ajax({
                        type: "POST",
                        url: reqth + "rotationchart/updateCommodityorder",
                        data: {
                            id:row.cid,
                            commodityorder:1
                        },
                        beforeSend: function() {
                            loadingIndex = layer.msg('处理中', {
                                icon: 16
                            });
                        },
                        success: function(result) {
                            layer.close(loadingIndex);
                            if (result.success) {
                                $("#rotationchartTable").bootstrapTable('refresh');
                                layer.msg("上架成功！", {
                                    time: 2000,
                                    icon: 6,
                                    shift: 6
                                }, function() {
                                });
                            } else {
                                layer.msg("上架失败！", {
                                    time: 2000,
                                    icon: 5,
                                    shift: 6
                                }, function() {});
                            }
                        }
                    });
                });
            },
            'click a[title=已上架]': function (e, value, row, index) {
                var loadingIndex = null;
                layer.confirm("确认下架"+row.commodityname+"吗？", {
                    icon: 3,
                    title: '提示'
                }, function(cindex) {
                    $.ajax({
                        type: "POST",
                        url: reqth + "rotationchart/updateCommodityorder",
                        data: {
                            id:row.cid,
                            commodityorder:0
                        },
                        beforeSend: function() {
                            loadingIndex = layer.msg('处理中', {
                                icon: 16
                            });
                        },
                        success: function(result) {
                            layer.close(loadingIndex);
                            if (result.success) {
                                $("#rotationchartTable").bootstrapTable('refresh');
                                layer.msg("下架成功！", {
                                    time: 2000,
                                    icon: 6,
                                    shift: 6
                                }, function() {
                                });
                            } else {
                                layer.msg("下架失败！", {
                                    time: 2000,
                                    icon: 5,
                                    shift: 6
                                }, function() {});
                            }
                        }
                    });
                });
            },
        }
    },]
});
function add(){
    $("#myModalLabel").text("添加");
    $("#cid").val("");
    $("#commodityname").val("");
    $("#commoditypath").val("");
    $("#commodityorder").val("");
    $("#commoditystate").val("");
    $("#statebackdrop").val("");
    $("#catid").val("");
    $("#mod-but").attr("onclick","insertById()");
}
function updateById(){
   cid=$("#cid").val();
   commodityname=$("#commodityname").val();
   commoditypath=$("#commoditypath").val();
   commodityorder=$("#commodityorder").val();
   commoditystate=$("#commoditystate").val();
   statebackdrop=$("#statebackdrop").val();
   catid = $("#catid").val();
    $.ajax({
        type:'post',
        url:reqth+"commodity/update",
        data:{
            cid:cid,
            commodityname:commodityname,
            commoditypath:commoditypath,
            commodityorder:commodityorder,
            commoditystate:commoditystate,
            statebackdrop:statebackdrop,
            catid:catid
        },success:function (result){
            if (result.success){
                $("#rotationchartTable").bootstrapTable('refresh');
                layer.msg("修改成功！", {
                    time: 2000,
                    icon: 6,
                    shift: 6
                });
            }else{
                layer.msg("修改失败！", {
                    time: 2000,
                    icon: 5,
                    shift: 6
                });
            }
        }
    })
}
function selector(){
    if (crid==4){
        return "shop/fruits";
    }else if(crid==5){
        return "shop/vegetables";
    }else if(crid==6){
       return "shop/seafood";
    }

}
function insertById(){
    cid=$("#cid").val();
    commodityname=$("#commodityname").val();
    commoditypath=$("#commoditypath").val();
    commodityorder=$("#commodityorder").val();
    commoditystate=$("#commoditystate").val();
    statebackdrop=$("#statebackdrop").val();
    catid = $("#catid").val();
    uploadFile(commodityname,selector());
    $.ajax({
        type: "POST",
        url: reqth + "commodity/add",
        data: {
            cid:cid,
            commodityname:commodityname,
            commoditypath:commoditypath,
            commodityorder:commodityorder,
            commoditystate:commoditystate,
            statebackdrop:statebackdrop,
            catid:catid
        },
        beforeSend: function() {
            loadingIndex = layer.msg('处理中', {
                icon: 16
            });
        },
        success: function(result) {
            layer.close(loadingIndex);
            $("#rotationchartTable").bootstrapTable('refresh');
            if (result.success) {
                layer.msg("添加成功！", {
                    time: 2000,
                    icon: 6,
                    shift: 6
                }, function() {
                });
            } else {
                layer.msg("添加失败！", {
                    time: 2000,
                    icon: 5,
                    shift: 6
                }, function() {});
            }
        }
    });

}

function uploadFile(name,path){
    var fileObj = document.getElementById("file");
    // H5 FormData 对象
    var form = new FormData();
    form.append("file", fileObj.files[0]); // 文件对象
    if (fileObj.files[0] == null) {
        // layer.alert("文件为空!")
        return false;
    }
    form.append("name",name);
    form.append("path", shopReq+path);
    // XMLHttpRequest 对象
    var xhr = new XMLHttpRequest();
    xhr.open("post", reqth + "Download/upload");
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            if (xhr.responseText == "true") {
                fileboolean = true;
            }
        }
    };
    xhr.send(form);
}